
data$forcing = as.integer(as.Date("1990-11-04"))- as.integer(as.Date(data$dob))
data$Cutoff08 = ifelse(data$forcing >= 0, 1, 0)

data$vote.gen2008_nonwhite = data$vote.gen2008 - data$vote.gen2008_white
data$vote.gen2010_nonwhite = data$vote.gen2010 - data$vote.gen2010_white
data$vote.gen2012_nonwhite = data$vote.gen2012 - data$vote.gen2012_white
data$vote.gen2014_nonwhite = data$vote.gen2014 - data$vote.gen2014_white
data$vote.gen2016_nonwhite = data$vote.gen2016 - data$vote.gen2016_white
data$nonwhite = data$registered - data$white
data$democrat_nonwhite = data$democrat - data$democrat_white
data$republican_nonwhite = data$republican - data$republican_white


data$lag2008_nonwhite = data$lag2008 - data$lag2008_white
data$lag2010_nonwhite = data$lag2010 - data$lag2010_white
data$lag2012_nonwhite = data$lag2012 - data$lag2012_white
data$lag2014_nonwhite = data$lag2014 - data$lag2014_black
data$lag2016_nonwhite = data$lag2016 - data$lag2016_black
data$lagregistered_nonwhite = data$registered - data$lagregistered_white
data$lagdem_nonwhite = data$lagdem - data$lagdem_white
data$lagrep_nonwhite = data$lagrep - data$lagrep_white




################# DV: Vote 2016 ########################

ivmod1_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                               downstream_voted = "vote.gen2016_white", downstream_lag = "lag2016_white", bandwidth = 365, state.abbrev=NULL)


ivmod1_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                            downstream_voted = "vote.gen2016_nonwhite", downstream_lag = "lag2016_nonwhite", bandwidth = 365, state.abbrev=NULL)


##################### DV: Vote 2014 ##########################

ivmod2_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "vote.gen2014_white", downstream_lag = "lag2014_white", bandwidth = 365, state.abbrev=NULL)
ivmod2_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "vote.gen2014_nonwhite", downstream_lag = "lag2014_nonwhite", bandwidth = 365, state.abbrev=NULL)


##################### DV: Vote 2012 ##########################

ivmod3_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "vote.gen2012_white", downstream_lag = "lag2012_white", bandwidth = 365, state.abbrev=NULL)
ivmod3_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "vote.gen2012_nonwhite", downstream_lag = "lag2012_nonwhite", bandwidth = 365, state.abbrev=NULL)




##################### DV: Vote 2010 ##########################


ivmod4_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "vote.gen2010_white", downstream_lag = "lag2010_white", bandwidth = 365, state.abbrev=NULL)
ivmod4_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "vote.gen2010_nonwhite", downstream_lag = "lag2010_nonwhite", bandwidth = 365, state.abbrev=NULL)


##################### DV: Democrat ##########################


ivmod5_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "democrat_white", downstream_lag = "lagdem_white", bandwidth = 365, state.abbrev=NULL)
ivmod5_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "democrat_nonwhite", downstream_lag = "lagdem_nonwhite", bandwidth = 365, state.abbrev=NULL)



##################### DV: Republican ##########################

ivmod_rep_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "republican_white", downstream_lag = "lagrep_white", bandwidth = 365, state.abbrev=NULL)
ivmod_rep_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "republican_nonwhite", downstream_lag = "lagrep_nonwhite", bandwidth = 365, state.abbrev=NULL)



##################### Registered ##########################


ivmod6_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_white", 
                                            downstream_voted = "white", downstream_lag = "lagregistered_white", bandwidth = 365, state.abbrev=NULL)
ivmod6_nonwhite = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote.gen2008_nonwhite", 
                                               downstream_voted = "nonwhite", downstream_lag = "lagregistered_nonwhite", bandwidth = 365, state.abbrev=NULL)







rm(data)
filename = paste0("results_", state, "_nonwhite.Rdata")
save.image(paste0("results/", filename))
rm(list=ls(pattern = "mod"))
